<?
include_once("db.php");

class ActiveRecode
{
	var $tablename;

	function select_($name, $value)/*{{{*/
	{
		$sql = "select * from ".$this->tablename." where `".$name."` = '".$value."'";
		$ar = get_rows(query($sql));
		return $ar[0];
	}/*}}}*/

	function update_($valuesinfield, $where)/*{{{*/
	{
		$values = $valuesinfield;
		$fields = array_keys($valuesinfield);
		$fields_lenght = count($fields);

		$update_sql = array();
		$i = 0;
		foreach($values as $item)
		{
			$update_sql[] .= "`".$fields[$i]."`='".$item."'";
			$i++;
		}

		$sql = "update ".$this->tablename." set ".implode(", ", $update_sql)." where ".$where;
		//echo "<br>".$sql."<br>";
		return query($sql);
	}/*}}}*/

	function insert_($valuesinfield)/*{{{*/
	{
		$values = $valuesinfield;
		$fields = array_keys($valuesinfield);
		
		array_walk($fields, "q", "`");
		array_walk($values, "q", "'");

		$fields_sql = implode(",", $fields); 
		$values_sql = implode(",", $values); 

		$sql = "insert into ".$this->tablename." (".$fields_sql.") values (".$values_sql.")";
		echo $sql;
		return query($sql);

	}/*}}}*/

	function delete_($where)/*{{{*/
	{
		$sql = "delete from ".$this->tablename." where ".$where;
		return query($sql);
	}/*}}}*/

	function query_($sql)/*{{{*/
	{
		return get_rows(query($sql));
	}/*}}}*/
}

?>
